import 'package:shared_preferences/shared_preferences.dart';

class Localstorage {
  static final Localstorage _instance = Localstorage._internal();
  static SharedPreferences? _prefs;

  factory Localstorage() => _instance;

  Localstorage._internal();

  static Future<SharedPreferences> get prefs async {
    _prefs ??= await SharedPreferences.getInstance();
    return _prefs!;
  }

  /// 存储字符串
  static Future<bool> setString(String key, String value) async {
    final storage = await prefs;
    return storage.setString(key, value);
  }

  /// 获取字符串
  static Future<String?> getString(String key, {String? defaultValue}) async {
    final storage = await prefs;
    return storage.getString(key) ?? defaultValue;
  }

  /// 存储整数
  static Future<bool> setInt(String key, int value) async {
    final storage = await prefs;
    return storage.setInt(key, value);
  }

  /// 获取整数
  static Future<int?> getInt(String key, {int? defaultValue}) async {
    final storage = await prefs;
    return storage.getInt(key) ?? defaultValue;
  }

  /// 存储布尔值
  static Future<bool> setBool(String key, bool value) async {
    final storage = await prefs;
    return storage.setBool(key, value);
  }

  /// 获取布尔值
  static Future<bool?> getBool(String key, {bool? defaultValue}) async {
    final storage = await prefs;
    return storage.getBool(key) ?? defaultValue;
  }

  /// 存储双精度浮点数
  static Future<bool> setDouble(String key, double value) async {
    final storage = await prefs;
    return storage.setDouble(key, value);
  }

  /// 获取双精度浮点数
  static Future<double?> getDouble(String key, {double? defaultValue}) async {
    final storage = await prefs;
    return storage.getDouble(key) ?? defaultValue;
  }

  /// 存储字符串列表
  static Future<bool> setStringList(String key, List<String> value) async {
    final storage = await prefs;
    return storage.setStringList(key, value);
  }

  /// 获取字符串列表
  static Future<List<String>?> getStringList(String key,
      {List<String>? defaultValue}) async {
    final storage = await prefs;
    return storage.getStringList(key) ?? defaultValue;
  }

  /// 删除指定键的数据
  static Future<bool> remove(String key) async {
    final storage = await prefs;
    return storage.remove(key);
  }

  /// 清空所有存储的数据
  static Future<bool> clear() async {
    final storage = await prefs;
    return storage.clear();
  }

  /// 检查是否包含指定的键
  static Future<bool> containsKey(String key) async {
    final storage = await prefs;
    return storage.containsKey(key);
  }

  /// 获取所有存储的键
  static Future<Set<String>> getKeys() async {
    final storage = await prefs;
    return storage.getKeys();
  }
}
